﻿using System;
using System.Data;
using System.Collections.Generic;
using DataLib.Common;
using DataLib.Model;
using System.Web;

namespace DataLib.BLL
{
	/// <summary>
	/// BarCode
	/// </summary>
	public partial class BarCode
	{
		private readonly DataLib.DAL.BarCode dal=new DataLib.DAL.BarCode();
		public BarCode()
		{}
		#region  BasicMethod

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int? id)
		{
			return dal.Exists(id);
		}

		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int  Add(DataLib.Model.BarCode model)
		{
			return dal.Add(model);
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(DataLib.Model.BarCode model)
		{
			return dal.Update(model);
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int? id)
		{
			
			return dal.Delete(id);
		}
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool DeleteList(string idlist )
		{
			return dal.DeleteList(idlist );
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public DataLib.Model.BarCode GetModel(int? id)
		{
			
			return dal.GetModel(id);
		}

		/// <summary>
		/// 得到一个对象实体，从缓存中
		/// </summary>
		public DataLib.Model.BarCode GetModelByCache(int? id)
		{
			
			string CacheKey = "BarCodeModel-" + id;
			object objModel = DataLib.Common.DataCache.GetCache(CacheKey);
			if (objModel == null)
			{
				try
				{
					objModel = dal.GetModel(id);
					if (objModel != null)
					{
						int ModelCache = DataLib.Common.ConfigHelper.GetConfigInt("ModelCache");
						DataLib.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
					}
				}
				catch{}
			}
			return (DataLib.Model.BarCode)objModel;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			return dal.GetList(strWhere);
		}
		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			return dal.GetList(Top,strWhere,filedOrder);
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public List<DataLib.Model.BarCode> GetModelList(string strWhere)
		{
			DataSet ds = dal.GetList(strWhere);
			return DataTableToList(ds.Tables[0]);
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public List<DataLib.Model.BarCode> DataTableToList(DataTable dt)
		{
			List<DataLib.Model.BarCode> modelList = new List<DataLib.Model.BarCode>();
			int rowsCount = dt.Rows.Count;
			if (rowsCount > 0)
			{
				DataLib.Model.BarCode model;
				for (int n = 0; n < rowsCount; n++)
				{
					model = dal.DataRowToModel(dt.Rows[n]);
					if (model != null)
					{
						modelList.Add(model);
				}
				}
			}
			return modelList;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetAllList()
		{
			return GetList("");
		}

		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			return dal.GetRecordCount(strWhere);
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			return dal.GetListByPage( strWhere,  orderby,  startIndex,  endIndex);
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		//public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		//{
			//return dal.GetList(PageSize,PageIndex,strWhere);
		//}

		#endregion  BasicMethod
		#region  ExtensionMethod
     
        /// <summary>
        /// 根据barcode返回Model
        /// </summary>
        /// <param name="barcode"></param>
        /// <returns></returns>
        public DataLib.Model.BarCode GetModelByCode(string barcode)
        {
            return dal.GetModelByCode(barcode);
        }

              /// <summary>
        /// 根据CJOID返回称重用的入库ID
        /// </summary>
        /// <param name="outid">出库单的ID作为Barcode的Parmentid</param>
        /// <returns></returns>
        public int GetInStroageIDByCjoid(string cjoid)
        {
            return dal.GetInStroageIDByCjoid(cjoid);
        }
               /// <summary>
        /// 根据来源的出库单ID返回已经入过库的ID号
        /// </summary>
        /// <param name="outid">出库单的ID作为Barcode的Parmentid</param>
        /// <returns></returns>
        public int GetInStroageIDByParmentid(string outid)
        {
            return dal.GetInStroageIDByParmentid(outid);
        }

        
        /// <summary>
        /// 统一修改BarCode的入库状态 以及 来源于出库单的处理重量
        /// </summary>
        /// <param name="instorageid">入库ID号</param>
        /// <returns>是否修改成功</returns>
        public bool UpdateByInsID(int instorageid)
        {
            return dal.UpdateByInsID(instorageid);
        }

        
        /// <summary>
        /// 更改状态为入临时库
        /// </summary>
        /// <param name="instorageid">入库单外键</param>
        /// <returns></returns>
        public bool UpdateByWrl(int instorageid)
        {
            return dal.UpdateByInsID(instorageid);
        }

               /// <summary>
        /// 根据临时库出库编号ID获取第一个符合的loadnumid
        /// </summary>
        /// <param name="outid"></param>
        /// <returns></returns>
        public int GetLoadIDByOutID(int outid)
        {
            return dal.GetLoadIDByOutID(outid);
        }

        
        /// <summary>
        /// LoadNum单的总重量及已入成品库总量计算
        /// </summary>
        /// <param name="lodnumid"></param>
        /// <param name="status">1为临时库入库，计总重，3为分类入库，当前完成重量</param>
        /// <returns></returns>
        public bool UpdateLoadWeight(int lodnumid, string status)
        {
            return dal.UpdateLoadWeight(lodnumid, status);
        }

               /// <summary>
        /// 更新库存及出库单状态
        /// </summary>
        /// <param name="inStoID">入库编号</param>
        /// <returns></returns>
        public bool UpdateStoragePostion(int inStoID, string oper)
        {
            return dal.UpdateStoragePostion(inStoID,oper);
        }

        /// <summary>
        /// 物品明细
        /// </summary>
        /// <param name="startdate">开始日期</param>
        /// <param name="enddate">结束日期</param>
        /// <param name="strbelong">仓库所属城市</param>
        /// <param name="strdis">仓库地理位置</param>
        /// <param name="gcode">物品编码</param>
        /// <param name="stroageno">库位号</param>
        /// <param name="flag">明细分类 1为入库 2为出库 3为期初　4为期末</param>
        /// <returns></returns>
        public DataSet GetDsByWhere(DateTime startdate, DateTime enddate, string strbelong, string strdis, string gcode, string stroageno, int flag)
        {
            //DateTime edate = enddate.AddDays(1).AddSeconds(-1);
            return dal.GetDsByWhere(startdate, enddate, strbelong, strdis, gcode, stroageno,flag);
        }

                /// <summary>
        /// 根据入库单ID获取
        /// </summary>
        /// <param name="instroageid">入库单id</param>
        /// <returns></returns>
        public DataSet GetCountList(string instroageid)
        {
            return dal.GetCountList(instroageid);
        }

                /// <summary>
        /// 合成BarCode
        /// </summary>
        /// <param name="barcodeValue"></param>
        /// <returns></returns>
        public Model.BarCode GetByValue(string barcodeValue)
        {
            return dal.GetByValue(barcodeValue);
        }

               /// <summary>
        /// 根据barcode的ID数组返回barcodevalue数组
        /// </summary>
        /// <param name="strid"></param>
        /// <returns></returns>
        public DataTable GetBarCodeValue(string strid)
        {
            return dal.GetBarCodeValue(strid);
        }


        /// <summary>
        /// 获取最新盘库入库编号
        /// </summary>
        /// <returns></returns>
        public string GetInsNo_StartStorage()
        {
            return dal.GetInsNo_StartStorage();
        }

               /// <summary>
        /// 获取最新库存盘点的Load编号
        /// </summary>
        /// <returns></returns>
        public string GetLoadNo_StartStorage()
        {
            return dal.GetLoadNo_StartStorage();
        }

              /// <summary>
        /// 获取最新条码编号
        /// </summary>
        /// <param name="LoadNo">虚拟Load号</param>
         /// <param name="GoodsCode">物品编码</param>
         /// <param name="belongcity">归属地</param>
         /// <param name="graphicity">处理地</param>
        /// <returns></returns>
        public string GetBarCode(string LoadNo, string GoodsCode, string belongcity, string graphicity)
        {
            return dal.GetBarCode(LoadNo, GoodsCode, belongcity, graphicity);
        }

   
        /// <summary>
        /// 插入BarCode后返回BarCode对象
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public Model.BarCode InsertBarcode(Model.BarCode model)
        {
            return dal.InsertBarcode(model);
        }

        
        /// <summary>
        /// 获得盘点列表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public DataTable GetPDList(string gcity, string bcity)
        {
            return dal.GetPDList(gcity, bcity);
        }

               /// <summary>
        /// 获取组合后的barcode
        /// </summary>
        /// <param name="instroageid">入库单id</param>
        /// <returns></returns>
        public DataSet GetBarList(string where)
        {
            return dal.GetBarList(where);
        }

        public DataTable GetDataList(string barcode, string baridarr, out string barid)
        {
            return dal.GetDataList(barcode, baridarr,out barid);

        }
		#endregion  ExtensionMethod
	}
}

